﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using GeneralLibs;

namespace Lists
{
    public class MayTinh : DoiTuong
    {
        #region OrderNumber property
        public int OrderNumber
        {
            get
            {
                return (int)_DataRow["OrderNumber"];
            }
            set
            {
                _DataRow["OrderNumber"] = value;
            }
        }
        #endregion

        #region Ten property
        public string Ten
        {
            get
            {
                return (string)_DataRow["Ten"];
            }
            set
            {
                _DataRow["Ten"] = value;
            }
        }
        #endregion

        #region HardwareID property
        public string HardwareID
        {
            get
            {
                return (string)_DataRow["HardwareID"];
            }
            set
            {
                _DataRow["HardwareID"] = value;
            }
        }
        #endregion

        public string GetSoMay()
        {
            if (OrderNumber <= 9)
                return "0" + OrderNumber.ToString();
            else
                return OrderNumber.ToString();
        }

        public void GenSoMayBaseOnDB()
        {
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();

                cmd.CommandText = string.Format("SELECT MAX(OrderNumber) MaxSoMay FROM MayTinh");
                SqlDataReader rd = cmd.ExecuteReader();
                if (rd.Read() && rd["MaxSoMay"] is int)
                {
                    int max = (int)rd["MaxSoMay"];
                    this.OrderNumber = max + 1;
                }
                else
                {
                    this.OrderNumber = 1;
                }
                rd.Close();
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(GeneralLibs.CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                this.OrderNumber = -1;
            }
            finally
            {
                con.Close();
            }
        }

        public MayTinh(DataRow dataRow):base(dataRow)
        {
        }
    }

    public class DanhMucMayTinh : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucMayTinh _Instance;
        public static DanhMucMayTinh Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucMayTinh();
                    //UniqueConstraint uniqueConstraint = new UniqueConstraint(_Instance.DataTable.Columns["Ma"]);
                    //_Instance.DataTable.Constraints.Add(uniqueConstraint);
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    _Instance.DataView.Sort = "ID";
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "MayTinh";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new MayTinh((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public MayTinh this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new MayTinh(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public MayTinh NewDoiTuong()
        {
            return new MayTinh(Instance.DataTable.NewRow());
        }

        public MayTinh GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new MayTinh(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public MayTinh GetByHardwareID(string cpuID)
        {
            try
            {
                return new MayTinh(DataTable.Select(string.Format("HardwareID = '{0}'", cpuID))[0]);
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public MayTinh GetByID(Guid objID)
        {
            return GetByProperty("ID", objID);
        }
        public MayTinh GetByOrderNumber(int orderNumber)
        {
            return GetByProperty("OrderNumber", orderNumber);
        }
        public MayTinh GetByOrderNumber(string orderNumber)
        {
            return GetByProperty("OrderNumber", orderNumber);
        }

        public static DanhMucMayTinh GetAll()
        {
            return Instance;
        }

        public static MayTinh GetCurrent()
        {
            MayTinh mt = DanhMucMayTinh.Instance.GetByHardwareID(GeneralLibs.LocalSettings.HeThong.HardwareID);
            return mt;
        }
    }
}
